//
// Created by v on 19-4-22.
//

#include "CMAutoTestCommand.h"
#include "CMTestKeyCommand.h"

CMAutoTestCommand::CMAutoTestCommand() {}

CMAutoTestCommand::~CMAutoTestCommand()
{
    for (auto &i : m_testCmd)
    {
        delete i.second;
    }
}

void CMAutoTestCommand::initAllCmd()
{

    registerCmd("key", new CMTestKeyCommand());
}

void CMAutoTestCommand::showUsage(const std::string &name)
{
    printf("Usage: %s\n", name.c_str());
    printf("    -a : Test all auto\n");
    printf("    -f : Open Fifo to communicate with cutedevice\n");
    printf("    -r [sec]: Send Standby command to cutedevice and reboot system "
           "after sometime\n");
    printf("    -c [cmd] : Test you give\n");
    printf("\n      Supported command: \n");
    for (auto &c : m_testCmd)
    {
        printf("%20s - ", c.first.c_str());
        c.second->showUsage();
        printf("\n");
    }
    printf("\n");
}

int CMAutoTestCommand::testStart(const std::string &name, int argc, char **args)
{
    if (name.length() == 0)
        return -1;
    CMCommandInstance *cmd = m_testCmd[name];
    int                ret = -1;
    if (cmd)
    {
        ret = cmd->startTest(argc, args);
    }
    else
    {
        LOGD("Auto test %s not implement\n", name.c_str());
    }
    return ret;
}

int CMAutoTestCommand::testAllAuto()
{
    int ret = -1;
    for (auto &c : m_testCmd)
    {
        CMCommandInstance *ii   = c.second;
        std::string        name = c.first;
        LOGD("====> auto test [%s] start\n", name.c_str());
        ret = ii->startTest(0, NULL);
        LOGD("====> auto test [%s] done with result [%d]\n\n", name.c_str(),
             ret);
    }
    return 0;
}
